package com.ogawa.chair7808.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.DownTimer;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Device7808StateBean;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.manager.TimeBean;
import com.ogawa.chair7808.observer.CloseAdvance;
import com.ogawa.chair7808.ui.iview.IFraAirView7506;
import com.ogawa.chair7808.ui.presenter.FraAirPresenter7506;
import com.ogawa.chair7808.view.MoveLayout;
import com.ogawa.chair7808.widget.LayoutBody7506;
import com.ogawa.chair7808.widget.LayoutBody7506L;
import com.ogawa.chair7808.widget.LayoutFunctionView7506;
import com.ogawa.chair7808.widget.LayoutGears5_7506;
import com.example.reslib.utils.PreferenceWrapper;

import java.util.ArrayList;
import java.util.List;

import static com.ogawa.base.Constant.Constant.DEVICE_7808Core;
import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_UPDATE_TIME;
import static com.ogawa.base.Constant.MqttConstant.LIVE_EVENT_BUS_KEY_RECEIVE_MESSAGE;
import static com.ogawa.base.utils.StringUtils.numberToTime;


/**
 * Created by Fhb on 2018/3/29.
 */
public class AirPressureMassageFragment extends BaseTimeTaskFragment implements View.OnClickListener, MoveLayout.OnChooseAction, IFraAirView7506 {

    private View inflate;

    private LinearLayout mLlAirPosition;

    private LayoutBody7506 mLayoutBody7506;
    private LayoutFunctionView7506 mLayoutFunction;

    private TextView mTvTime;
    private LayoutGears5_7506 mLlAirStrong;

    private ImageView mIvShoulder;
    private ImageView mIvBack;
    private ImageView mIvHand;
    private ImageView mIvLeg;
    private ImageView mIvAll;
    private List<ImageView> mIvAirPosition = new ArrayList<>();
    private String[] name = {"充气位置", "充气强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private final long INTERVAL = 1000L;
//    private FraAirPresenter7506 mPresenter;
    private long mPauseTime;
    private boolean isPause;
    private ViewStub mView7506;
    private ViewStub mView7506L;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_air_pressure_massage_7808, container, false);
        initView();

        LiveEventBus.get(LIVE_EVENT_BUS_KEY_UPDATE_TIME, TimeBean.class).observe(this, new androidx.lifecycle.Observer<TimeBean>() {
            @Override
            public void onChanged(TimeBean timeBean) {
                long min = timeBean.getMin();
                long second = timeBean.getSecond();
                mTvTime.setText("" + numberToTime(min) + " : " + numberToTime(second));
            }
        });

        LiveEventBus.get(LIVE_EVENT_BUS_KEY_RECEIVE_MESSAGE).observe(this, new androidx.lifecycle.Observer<Object>() {
            @Override
            public void onChanged(Object integer) {
                updateView();

            }
        });

        return inflate;
    }


    private void initView() {
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        mIvShoulder = inflate.findViewById(R.id.fragment_air_shoulder);
        mIvBack = inflate.findViewById(R.id.fragment_mt_iv_back);
        if (mChoose == DEVICE_7808Core) {
            mIvBack.setImageResource(R.drawable.selector_gas_waist);
        }
        mIvHand = inflate.findViewById(R.id.fragment_mt_iv_hand);
        mIvLeg = inflate.findViewById(R.id.fragment_mt_iv_leg);
        mIvAll = inflate.findViewById(R.id.fragment_mt_iv_all);
        mScroll = inflate.findViewById(R.id.fragment_air_scoll);
        mLayout = inflate.findViewById(R.id.air_fragment_move_layout);
        mTvTime = inflate.findViewById(R.id.fragment_air_tv_time);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);
        mIvAirPosition.clear();
        mIvAirPosition.add(mIvShoulder);
        mIvAirPosition.add(mIvBack);
        mIvAirPosition.add(mIvHand);
        mIvAirPosition.add(mIvLeg);
        mIvAirPosition.add(mIvAll);


        mLlAirPosition = inflate.findViewById(R.id.linear_methods_to_select);
        mLlAirStrong = inflate.findViewById(R.id.rl_air_strong);

        mLlAirStrong.setType(3);
//        if (!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong))) {
//            Log.e("setsetstring", DataManager.getInst().getValue(CommmandNum.mStatusAirStrong) + "");
//            mLlAirStrong.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong)));
//        }
        mView7506 = inflate.findViewById(R.id.layout_body_view);
        mView7506L = inflate.findViewById(R.id.layout_body_view_7506_l);
        if (mLayoutBody7506 == null) {
            mLayoutBody7506 = (LayoutBody7506) mView7506.inflate();
        }
        mLayoutFunction = inflate.findViewById(R.id.fragment_air_layout_function);
//        mPresenter = new FraAirPresenter7506(this);
        mTvTime.setOnClickListener(this);


        mIvShoulder.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mIvHand.setOnClickListener(this);
        mIvLeg.setOnClickListener(this);
        mIvAll.setOnClickListener(this);
//        initData();
    }

//    public void initData() {
//        mPresenter.getAirPosition(CommmandNum.mStatusAirShoulder, DataManager.getInst().getValue(CommmandNum.mStatusAirShoulder));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirSit, DataManager.getInst().getValue(CommmandNum.mStatusAirSit));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirHand, DataManager.getInst().getValue(CommmandNum.mStatusAirHand));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirLeg, DataManager.getInst().getValue(CommmandNum.mStatusAirLeg));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirAll, DataManager.getInst().getValue(CommmandNum.mStatusAirAll));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirStrong, DataManager.getInst().getValue(CommmandNum.mStatusAirStrong));
//    }


    private void updateView(){
        Device7808StateBean device7808StateBean = DataManager.getInst().getDevice7808StateBean();
        mLlAirStrong.setShow(Integer.valueOf(device7808StateBean.getAirIntensity()));//充气强度
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
//        mPresenter.onDestory();
        mLayoutFunction.onDestory();
        if (mLayoutBody7506 != null) {
            mLayoutBody7506.onDestory();
        }
//        cancelTime();

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fragment_air_tv_time) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {

                showDialog();
            }

        } else if (i == R.id.fragment_air_shoulder) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.AIR_JIANB,"2");

            }

        } else if (i == R.id.fragment_mt_iv_back) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.AIR_BEIB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_hand) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.AIR_SHOUB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_leg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.AIR_TUIB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_all) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.AIR_ALL,"2");
            }

        }
    }


    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position) {
            case 0:
                mLlAirPosition.setVisibility(View.VISIBLE);
                mLlAirStrong.setVisibility(View.GONE);
                break;
            case 1:
                mLlAirPosition.setVisibility(View.GONE);
                mLlAirStrong.setVisibility(View.VISIBLE);
                break;
        }

    }

    private void setPosition(int position, boolean isSelect) {
        Log.e("pisitionnnn", position + "");
        mIvAirPosition.get(position).setSelected(isSelect);
    }

    @Override
    public void getTime(String time) {
        sendTime(time);
    }

    @Override
    public void getAirPosition(int position, int isRun) {
        setPosition(position, isRun == 1);
    }

    @Override
    public void getAirStrong(int strong) {
        mLlAirStrong.setShow(strong);
    }

//    public void pauseTime() {
//        if (mTimer != null) {
//            mTimer.pause();
//        }
//
//    }
//
//    public void reStarTime() {
//        if (mTimer != null) {
//            mTimer.resume();
//        }
//
//
//    }
//
//    public void cancelTime() {
//        if (mTimer != null) {
//            mTimer.cancel();
//        }
//
//
//    }
//
//    public void setTime(int min, int sec) {
//        cancelTime();
//        if (DataManager.getInst().isAdvanceRunning() || DataManager.getInst().isAutoRunning()) {
//            mTimer = new DownTimer();//实例化
//            mTimer.setTotalTime(min * 60 * 1000 + sec * 1000);//设置毫秒数
//            mTimer.setIntervalTime(1000);//设置间隔数
//            mTimer.setTimerLiener(new DownTimer.TimeListener() {
//                @Override
//                public void onFinish() {
//                    mTvTime.setText("00 : 00");
//                    mTime = 0;
//                    CloseAdvance.getInst().noticeonClose();
//                }
//
//                @Override
//                public void onInterval(long time) {
//                    long showTime = time / 1000;
//                    mTime = showTime;
//                    if (showTime <= 59) {
//                        mTvTime.setText(String.format("00:%02d", showTime));
//                    } else {
//                        mTvTime.setText(String.format("%02d:%02d", showTime / 60, showTime % 60));
//                    }
//                    if (!DataManager.getInst().isAdvanceRunning() && DataManager.getInst().isAutoRunning()) {
//                        mTimer.cancel();
//                        mTvTime.setText("00 : 00");
//                        mTime = 0;
//                    }
//                }
//            });
//            mTimer.start();
//        } else {
//            mTimer.cancel();
//            mTvTime.setText("00 : 00");
//            mTime = 0;
//        }
//
//    }

}
